import { Context } from 'koa';
import { calculateY, calculateOff } from '../utils/calculator';

interface CalculatorRequestBody {
    profitValue: number;
    profitType: 'fixed' | 'cost' | 'price';
    cost: number;
    _coupon: number;
    off?: number;
    y?: number;
    weight: number;
    withdrawalFeeRate?: number;
    platformCommissionRate?: number;
    sfpFeeRate?: number;
    talentCommissionRate?: number;
    transactionFeeRate?: number;
    vatRate?: number;
    bonusCashbackRate?: number;
    logisticsCostPer10g?: number;
    exchangeRate?: number;
    buyerShippingFee?: number;
}

export async function calculateYController(ctx: Context) {
    try {
        const body = ctx.request.body as CalculatorRequestBody;
        const {
            profitValue,
            profitType,
            cost,
            _coupon,
            off,
            weight,
            withdrawalFeeRate,
            platformCommissionRate,
            sfpFeeRate,
            talentCommissionRate,
            transactionFeeRate,
            vatRate,
            bonusCashbackRate,
            logisticsCostPer10g,
            exchangeRate,
            buyerShippingFee
        } = body;

        const result = calculateY(
            Number(profitValue),
            profitType,
            Number(cost),
            Number(_coupon),
            Number(off),
            Number(weight),
            Number(withdrawalFeeRate),
            Number(platformCommissionRate),
            Number(sfpFeeRate),
            Number(talentCommissionRate),
            Number(transactionFeeRate),
            Number(vatRate),
            Number(bonusCashbackRate),
            Number(logisticsCostPer10g),
            Number(exchangeRate),
            Number(buyerShippingFee)
        );

        ctx.body = {
            success: true,
            data: result
        };
    } catch (error: any) {
        ctx.status = 400;
        ctx.body = {
            success: false,
            error: error?.message || '计算失败'
        };
    }
}

export async function calculateOffController(ctx: Context) {
    try {
        const body = ctx.request.body as CalculatorRequestBody;
        const {
            y,
            profitValue,
            profitType,
            cost,
            _coupon,
            weight,
            withdrawalFeeRate,
            platformCommissionRate,
            sfpFeeRate,
            talentCommissionRate,
            transactionFeeRate,
            vatRate,
            bonusCashbackRate,
            logisticsCostPer10g,
            exchangeRate,
            buyerShippingFee
        } = body;

        const result = calculateOff(
            Number(y),
            Number(profitValue),
            profitType,
            Number(cost),
            Number(_coupon),
            Number(weight),
            Number(withdrawalFeeRate),
            Number(platformCommissionRate),
            Number(sfpFeeRate),
            Number(talentCommissionRate),
            Number(transactionFeeRate),
            Number(vatRate),
            Number(bonusCashbackRate),
            Number(logisticsCostPer10g),
            Number(exchangeRate),
            Number(buyerShippingFee)
        );

        ctx.body = {
            success: true,
            data: result
        };
    } catch (error: any) {
        ctx.status = 400;
        ctx.body = {
            success: false,
            error: error?.message || '计算失败'
        };
    }
} 